package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;
import androidx.annotation.NonNull;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.listener.ItemClickListener;
import com.yoomiito.app.model.home.PanicTime;
import com.yoomiito.app.widget.CenterLayoutManager;

import java.util.List;

public class HomePanicBuyTitleAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private RecyclerView.RecycledViewPool viewPool;
    private List<PanicTime> mPanicTimes;
    private HomeTimeAdapter homeTimeAdapter;
    private int itemSize;
    private ItemClickListener mItemClickListener;
    private int clickPosition;
    private boolean update;

    public HomePanicBuyTitleAdapter(Context context, int itemSize, LayoutHelper layoutHelper, RecyclerView.RecycledViewPool viewPool, List<PanicTime> mPanicTimes) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        this.viewPool = viewPool;
        this.mPanicTimes = mPanicTimes;
        this.itemSize = itemSize;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.panic_buy_title, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        if (viewHolder.rcy.getChildCount() == 0) {
            viewHolder.rcy.setRecycledViewPool(viewPool);
            LinearLayoutManager layoutManager = new CenterLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
            viewHolder.rcy.setLayoutManager(layoutManager);
            homeTimeAdapter = new HomeTimeAdapter(mPanicTimes);
            viewHolder.rcy.setAdapter(homeTimeAdapter);
            homeTimeAdapter.setOnItemChildClickListener((adapter, view, position1) -> {
                clickPosition = position1;
                viewHolder.rcy.smoothScrollToPosition(position1);
                notifyItem(position1);
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(position1);
                }
            });
            viewHolder.rcy.smoothScrollToPosition(clickPosition);
        }else if (update){//更新
            viewHolder.rcy.smoothScrollToPosition(clickPosition);
            homeTimeAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return itemSize;
    }

    @Override
    public int getItemViewType(int position) {
        return 8;
    }

    public void notifyItem(int position) {
        if (mPanicTimes != null) {
            for (int i = 0; i < mPanicTimes.size(); i++) {
                PanicTime panicTime = mPanicTimes.get(i);
                panicTime.setSelected(i == position ? true : false);
            }
            homeTimeAdapter.notifyDataSetChanged();
        }
    }
    public void setTimePosition(int clickPosition){
        this.clickPosition = clickPosition;
    }

    public void setItemSize(int itemSize) {
        this.itemSize = itemSize;
    }

    public void setUpdate(boolean update) {
        this.update = update;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        private RecyclerView rcy;

        public ItemViewHolder(View itemView) {
            super(itemView);
            rcy = itemView.findViewById(R.id.rcy);
        }
    }
}
